﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ChucNang_KhachHangThanThiet_KHTT_XemThongTinDonDatHang : System.Web.UI.Page
{
    int RowPerPage;
    int startIndex;
    int page;
    GlobalFunctions gf;
    int idtaikhoan;
    protected void Page_Load(object sender, EventArgs e)
    {
        gf = new GlobalFunctions();
        if (gf.IsLogin() != 1)
        {
            Response.Redirect("/SMO/ChucNang/NguoiDung/NguoiDung_DangNhap.aspx");
        }
        if (gf.IsKHTT() != 1)
        {
            Response.Write("<script>alert('Bạn phải là khách hàng thân thiết mới thực hiện mua sản phẩm online!')</script>");
            Response.Redirect("/SMO/ChucNang/NguoiDung/NguoiDung_DangNhap.aspx");
        }
        idtaikhoan = gf.getLoginId();

        if (Request["page"] != null)
            page = int.Parse(Request["page"]);
        else
            page = 0;
      
        RowPerPage = int.Parse(ConfigurationSettings.AppSettings["RowPerPage"]);
        startIndex = (page * RowPerPage);
     
        int sapxep = 1;
        if (Session["SapXep"] != null)
            sapxep = int.Parse(Session["SapXep"].ToString());
        if (!this.IsPostBack)
        {
            ddlSapXep.SelectedValue = sapxep.ToString();
        }
        DataDSTableAdapters.DATHANGTableAdapter dhadapter = new DataDSTableAdapters.DATHANGTableAdapter();
        PageInterface1.currentPageName = GlobalFunctions.GetCurrentPageName();
        PageInterface1.currentPageNumber = page;
        PageInterface1.totalNumberOfPage = (int)dhadapter.DATHANG_LayTongSoDonHangTheoIdTaiKhoan(idtaikhoan) / RowPerPage;
        if ((int)dhadapter.DATHANG_LayTongSoDonHangTheoIdTaiKhoan(idtaikhoan) % RowPerPage != 0)
            PageInterface1.totalNumberOfPage++;
        gf = new GlobalFunctions();
        DataList_DanhSachSanPham.DataSource = dhadapter.DATHANG_LayDonDatHangTheoTaiKhoan(idtaikhoan, startIndex, RowPerPage, sapxep);
        DataList_DanhSachSanPham.DataBind();
    }
    protected void DataList_DanhSachSanPham_ItemCreated(object sender, DataListItemEventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataListItem item = e.Item;
            DateTime thoigiandathang = (DateTime)((DataRowView)item.DataItem).Row["ThoiGianDatHang"];
            string thoigian=thoigiandathang.ToString("dd/M/yyyy HH:MM:s");
            Label lblthoigiandathang=(Label) item.FindControl("lblThoiGianDatHang");
            lblthoigiandathang.Text = thoigian;

             Label lblthoigiangiaohang=(Label) item.FindControl("lblThoiGianGiaoHang");
             Object ob =((DataRowView)item.DataItem).Row["NgayDaGiaoHang"];
             if (ob.ToString() == "")
             {
                 int trangthai1 = (int)((DataRowView)item.DataItem).Row["TrangThai"];
                 switch (trangthai1)
                 {
                     case 1:
                         lblthoigiangiaohang.Text = "Đã Hủy";
                         break;
                     case 2:
                         lblthoigiangiaohang.Text = "Đang chờ giao";
                         break;
                     case 4:
                         lblthoigiangiaohang.Text = "Chưa giao";
                         break;
                 }
             }
             else
             {
                 DateTime thoigiangiaohang = (DateTime)ob;
                 lblthoigiangiaohang.Text = thoigiangiaohang.ToString("dd/M/yyyy HH:MM:s");
             }
             Label lbltongtien = (Label)item.FindControl("lblTongTien");
             lbltongtien.Text = ((DataRowView)item.DataItem).Row["TongTien"].ToString() + " VND";

             Label lbltrangthai = (Label)item.FindControl("lblTrangThai");
             int trangthai = int.Parse(((DataRowView)item.DataItem).Row["TrangThai"].ToString());
             Label lblhuy = (Label)item.FindControl("lblHuy");
             switch (trangthai)
             {
                 case 1:
                     lbltrangthai.Text = "Đã hủy lúc" + ((DateTime)((DataRowView)item.DataItem).Row["ThoiGianDatHang"]).ToString("dd/M/yyyy HH:MM:s");
                     lblhuy.Text = "<input type='checkbox' name='chkXoa' value='" + ((DataRowView)item.DataItem).Row["Id"].ToString() + "' >Xóa</input>";
                     break;
                 case 2:
                     lbltrangthai.Text = "Đang chờ giao";
                     lblhuy.Text = "<input type='checkbox' name='chkHuy' value='" + ((DataRowView)item.DataItem).Row["Id"].ToString()+ "' >Hủy</input>";
                     break;
                 case 3:
                     lbltrangthai.Text = "Đã Giao";
                     lblhuy.Text = "<input type='checkbox' name='chkXoa' value='" + ((DataRowView)item.DataItem).Row["Id"].ToString()+"' >Xóa</input>";
                     break;
                 case 4:
                     lbltrangthai.Text = "Chưa Giao";
                     lblhuy.Text = "<input type='checkbox' name='chkHuy' value='" + ((DataRowView)item.DataItem).Row["Id"].ToString() + "' >Hủy</input>";
                     break;
                 default:
                     lbltrangthai.Text = "Ko biết";
                     lblhuy.Text = "";
                     break;
             }
                

        }
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        string[] sid = Request.Form.GetValues("chkHuy");
        if (sid == null)
        {
            Response.Redirect("KHTT_XemThongTinDonDatHang.aspx?page" + page.ToString() +"&rowperpage="+RowPerPage.ToString());
        }
        else
        {
            string chuoihuy = "";
            for (int i = 0; i < sid.Length; i++)
            {
                if (i == 0)
                    chuoihuy += sid[i];
                else
                    chuoihuy += "_" + sid[i];
            }
            Session["Huy"] = chuoihuy;
            Response.Redirect("KHTT_HuyDonHang.aspx");
        }
        
    }
    protected void ddlSapXep_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["SapXep"] = ddlSapXep.SelectedValue;
        Response.Redirect(GlobalFunctions.GetCurrentPageName());
    }
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        string[] sid = Request.Form.GetValues("chkXoa");
        if (sid == null)
        {
            Response.Redirect(Request.UrlReferrer.ToString());
        }
        else
        {
            DataDSTableAdapters.DATHANGTableAdapter dhadapter = new DataDSTableAdapters.DATHANGTableAdapter();
            for (int i = 0; i < sid.Length; i++)
            {
                dhadapter.DATHANG_XoaTheoId_IdTaiKhoan(int.Parse(sid[i]),idtaikhoan);
            }
            Response.Write("<script>alert('Xóa thành công');location.href='" + Request.UrlReferrer.ToString() + "'</script>");
            
        }
    }
}
